package com.ydjk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ydjk.common.utils.TemplateExcelUtils;
import com.ydjk.domain.JkUser;
import com.ydjk.service.IJkUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ydjk.common.annotation.Log;
import com.ydjk.common.core.controller.BaseController;
import com.ydjk.common.core.domain.AjaxResult;
import com.ydjk.common.enums.BusinessType;
import com.ydjk.domain.JkFllowup;
import com.ydjk.service.IJkFllowupService;
import com.ydjk.common.utils.poi.ExcelUtil;
import com.ydjk.common.core.page.TableDataInfo;

/**
 * 随访管理Controller
 *
 * @author ydjk
 * @date 2023-08-30
 */
@RestController
@RequestMapping("/user/fllowup")
public class JkFllowupController extends BaseController
{
    @Autowired
    private IJkFllowupService jkFllowupService;

    @Autowired
    private IJkUserService userService;

    /**
     * 查询随访管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:fllowup:list')")
    @GetMapping("/list")
    public TableDataInfo list(JkFllowup jkFllowup)
    {
        startPage();
        jkFllowup.setIfKnow(1);
        List<JkFllowup> list = jkFllowupService.selectJkFllowupList(jkFllowup);
        return getDataTable(list);
    }

    /**
     * 导出随访管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:fllowup:export')")
    @Log(title = "随访管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JkFllowup jkFllowup) throws Exception {
        jkFllowup.setIfKnow(1);
        List<JkFllowup> list = jkFllowupService.selectJkFllowupList(jkFllowup);
        ExcelUtil<JkFllowup> util = new ExcelUtil<JkFllowup>(JkFllowup.class);
        util.exportExcel(response, list, "随访管理数据");
//        Map<String, Object> param = new HashMap<>();
//        param.put("list", list);
//        TemplateExcelUtils.downLoadExcel("test","template_fllowup.xls",param,response);
    }

    /**
     * 获取随访管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:fllowup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jkFllowupService.selectJkFllowupById(id));
    }

    /**
     * 新增随访管理
     */
    @PreAuthorize("@ss.hasPermi('user:fllowup:add')")
    @Log(title = "随访管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JkFllowup jkFllowup)
    {
        return toAjax(jkFllowupService.insertJkFllowup(jkFllowup));
    }

    /**
     * 修改随访管理
     */
    @PreAuthorize("@ss.hasPermi('user:fllowup:edit')")
    @Log(title = "随访管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JkFllowup jkFllowup)
    {
        return toAjax(jkFllowupService.updateJkFllowup(jkFllowup));
    }

    /**
     * 删除随访管理
     */
    @PreAuthorize("@ss.hasPermi('user:fllowup:remove')")
    @Log(title = "随访管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jkFllowupService.deleteJkFllowupByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('user:fllowup:query')")
    @GetMapping(value = "/getDetailWithUser/{id}")
    public AjaxResult getDetailWithUser(@PathVariable("id") Long id)
    {
        JkFllowup jkFllowup = jkFllowupService.selectJkFllowupById(id);
        JkUser jkUser = userService.selectJkUserById(jkFllowup.getUserId());
        return AjaxResult.success().put("userInfo",jkUser).put("fllowupInfo",jkFllowup);
    }
}
